﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Common;

namespace CodeFactory
{
    public partial class NewProjectFM : Form
    {
        MainStudio ms;

        private int index = 1;

        public NewProjectFM()
        {
            InitializeComponent();
        }
        public NewProjectFM(MainStudio _ms)
        {
            InitializeComponent();
            ms = _ms;
        }
        private void UpdateExitsProjectList()
        {
           lbExitsProject.Items.Clear();

           foreach(string sName in Directory.GetDirectories(PubConstant.GetProjectPath()))
           {
               lbExitsProject.Items.Add(sName.Split('\\')[(sName.Split('\\').GetLength(0)-1)]);

           }
        }

        private void CreateFolderName()
        {
            if (Directory.Exists(PubConstant.GetProjectPath() + txtProjectName.Text.Trim()))
            {
                index++;
                txtProjectName.Text = "NewProject" + index;
                CreateFolderName();
            }
        }

        public string GetProjectName()
        {
            try
            {
                return txtProjectName.Text;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        private void NewProjectFM_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                CreateFolderName();
            }
        }

        private void NewProjectFM_Load(object sender, EventArgs e)
        {
            UpdateExitsProjectList();
            txtProjectName.Text = "NewProject" + index;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(PubConstant.GetProjectPath() + txtProjectName.Text.Trim()))
            {
                MessageBox.Show("已经存在此工程！");
                return;
            }
            else 
            {
                Directory.CreateDirectory(PubConstant.GetProjectPath() + txtProjectName.Text.Trim());
                this.Visible = false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }
    }
}
